﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Win32;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Media;
using 充电桩上位机.Model.PEM;
using 充电桩上位机.View.MarkBook;

namespace 充电桩上位机.ViewModel.ServerRegisters;

public class ServerRegistersViewModel : ObservableObject
{
    private string _stackText = string.Empty;
    public string StackText
    {
        get => _stackText;
        set => SetProperty(ref _stackText, value);
    }
    private bool _offlineCheckedBack = false;
    public bool OfflineCheckedBack
    {
        get => _offlineCheckedBack;
        set => SetProperty(ref _offlineCheckedBack, value);
    }

    #region 功能类
    [Category("OCPP 地址")]
    private string _oCPPServer = string.Empty;
    public string OCPPServer
    {
        get => _oCPPServer;
        set {
            var favor = value.Split('/').LastOrDefault()!;

            if (favor != null)
            {
                StackText = favor;
            }
            SetProperty(ref _oCPPServer, value); 
        }
    }    
    #endregion
    [Category("WIFI按下颜色")]
    private Brush _hTTPState = Brushes.Black;
    public Brush HTTPState
    {
        get => _hTTPState;
        set => SetProperty(ref _hTTPState, value);
    }
    [Category("WIFI按下状态")]
    private bool _hTTPChecked = false;
    public bool HTTPChecked
    {
        get => _hTTPChecked;
        set
        {
            if (value)
                HTTPState = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2563EB"));
            else
                HTTPState = Brushes.Black;
            SetProperty(ref _hTTPChecked, value);
        }
    }
    [Category("HTTP 用户名")]
    private string _httpUserName = string.Empty;
    public string HttpUserName
    {
        get => _httpUserName;
        set => SetProperty(ref _httpUserName, value);
    }
    [Category("HTTP 密码")]
    private string _httpUserPassword = string.Empty;
    public string HttpUserPassword
    {
        get => _httpUserPassword;
        set => SetProperty(ref _httpUserPassword, value);
    }
    [Category("ChangePointModel（OCPP的）")]
    private string _changePointModel = string.Empty;
    public string ChangePointModel
    {
        get => _changePointModel;
        set => SetProperty(ref _changePointModel, value);
    }
    [Category("ChargePointVendor")]
    private string _chargePointVendor = string.Empty;
    public string ChargePointVendor
    {
        get => _chargePointVendor;
        set => SetProperty(ref _chargePointVendor, value);
    }
    [Category("QR_Code_1")]
    private string _qR_Code_1 = string.Empty;
    public string QR_Code_1
    {
        get => _qR_Code_1;
        set => SetProperty(ref _qR_Code_1, value);
    }
    [Category("QR_Code_2")]
    private string _qR_Code_2 = string.Empty;
    public string QR_Code_2
    {
        get => _qR_Code_2;
        set => SetProperty(ref _qR_Code_2, value);
    }
    [Category("QR_Code_3")]
    private string _qR_Code_3 = string.Empty;
    public string QR_Code_3
    {
        get => _qR_Code_3;
        set => SetProperty(ref _qR_Code_3, value);
    }

    
    [Category("Z-BOX按下颜色")]
    private Brush _zBoxColor = Brushes.Black;
    public Brush ZBoxColor
    {
        get => _zBoxColor;
        set => SetProperty(ref _zBoxColor, value);
    }
    [Category("Z-BOX按下状态")]
    private bool _zBoxCheck = false;
    public bool ZBoxCheck
    {
        get => _zBoxCheck;
        set
        {
            if (value)
            {
                OtherIsSet = false;
                ZBoxColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2563EB"));
                OCPPServer = "wss://zbox.evocpp.com/ocpp/evid";
            }
            else
                ZBoxColor = Brushes.Black;
            SetProperty(ref _zBoxCheck, value);
        }
    }

    [Category("EVSaas按下颜色")]
    private Brush _eVSaasColor = Brushes.Black;
    public Brush EVSaasColor
    {
        get => _eVSaasColor;
        set => SetProperty(ref _eVSaasColor, value);
    }
    [Category("EVSaas按下状态")]
    private bool _eVSaasCheck = false;
    public bool EVSaasCheck
    {
        get => _eVSaasCheck;
        set
        {
            if (value)
            {
                OtherIsSet = false;
                EVSaasColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2563EB"));
                OCPPServer = "wss://www.evocpp.com/ocpp/evid";
            }
            else
                EVSaasColor = Brushes.Black;
            SetProperty(ref _eVSaasCheck, value);
        }
    }
    [Category("Steve1按下颜色")]
    private Brush _steve1Color = Brushes.Black;
    public Brush Steve1Color
    {
        get => _steve1Color;
        set => SetProperty(ref _steve1Color, value);
    }
    [Category("Steve1按下状态")]
    private bool _steve1Check = false;
    public bool Steve1Check
    {
        get => _steve1Check;
        set
        {
            if (value)
            {
                OtherIsSet = false;
                Steve1Color = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2563EB"));
                OCPPServer = "ws://47.97.181.104:8080/steve/websocket/CentralSystemService/(chargeBoxId)";
            }
            else
                Steve1Color = Brushes.Black;
            SetProperty(ref _steve1Check, value);
        }
    }
    [Category("Steve2按下颜色")]
    private Brush _steve2Color = Brushes.Black;
    public Brush Steve2Color
    {
        get => _steve2Color;
        set => SetProperty(ref _steve2Color, value);
    }
    [Category("Steve2按下状态")]
    private bool _steve2Check = false;
    public bool Steve2Check
    {
        get => _steve2Check;
        set
        {
            if (value)
            {
                OtherIsSet = false;
                Steve2Color = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2563EB"));
                OCPPServer = "ws://47.101.173.122:8080/steve/websocket/CentralSystemService/(chargeBoxId)";
            }
            else
                Steve2Color = Brushes.Black;
            SetProperty(ref _steve2Check, value);
        }
    }

    [Category("Monts按下颜色")]
    private Brush _montsColor = Brushes.Black;
    public Brush MontsColor
    {
        get => _montsColor;
        set => SetProperty(ref _montsColor, value);
    }
    [Category("Monts按下状态")]
    private bool _montsCheck = false;
    public bool MontsCheck
    {
        get => _montsCheck;
        set
        {
            if (value)
            {
                OtherIsSet = false;
                MontsColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2563EB"));
                OCPPServer = "ws://monitor.evocpp.com/evid";
            }
            else
                MontsColor = Brushes.Black;
            SetProperty(ref _montsCheck, value);
        }
    }


    [Category("Ohter按下颜色")]
    private Brush _otherSetColor = Brushes.Black;
    public Brush OtherSetColor
    {
        get => _otherSetColor;
        set => SetProperty(ref _otherSetColor, value);
    }

    [Category("Ohter 按钮被按下")]
    private bool _otherIsSet = false;
    public bool OtherIsSet
    {
        get => _otherIsSet;
        set {
            if (value)
            {
                var savestring = OCPPServer;
                Steve1Check = false;
                Steve2Check = false;
                MontsCheck = false;
                EVSaasCheck = false;
                ZBoxCheck = false;
                OCPPServer = savestring;
                OtherSetColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2563EB"));
            }
            else
                OtherSetColor = Brushes.Black;
            SetProperty(ref _otherIsSet, value); 
        }
    }

    [Category("地址显示框")]
    private string _addressText = string.Empty;
    public string AddressText
    {
        get => _addressText;
        set => SetProperty(ref _addressText, value);
    }

    [Category("证书打开按钮")]
    public ICommand OpenFileCommand =>
    new RelayCommand(async () => await OpenFileCommandFunc());
    [Category("服务寄存器设置界面逻辑")]
    private Task OpenFileCommandFunc()
    {
        //对已经描述的地址进行读取
        new MarkBookView().Show();
        return Task.CompletedTask;
    }
    [Category("获取证书地址")]
    public ICommand AddressGetCommand =>
    new RelayCommand(async () => await AddressGetCommandFunc());

    [Category("传参状态")]
    public static bool _bookNeedWhereState = false;
    public bool BookNeedWhereState
    {
        get => _bookNeedWhereState;
        set => SetProperty(ref _bookNeedWhereState, value);
    }
    [Category("获取证书地址界面逻辑")]
    private async Task AddressGetCommandFunc()
    {
        //对已经描述的地址进行读取
        OpenFileDialog openFileDialog = new()
        {
            //Filter = "xls file|*.xlsx",
            //DefaultExt = ".xlsx"
        };
        if (openFileDialog.ShowDialog() == true)
        {
            //clear 并进行填充
            await System.Windows.Application.Current.Dispatcher.InvokeAsync(delegate
            {
                AddressText = openFileDialog.FileName;                
            });
            //打开该文件并将内容加载进PTA

        }
    }
    #region 全球化
    [Category("标题")]
    public string _server_Registers_Text = string.Empty;
    public string Server_Registers_Text
    {
        get => _server_Registers_Text;
        set => SetProperty(ref _server_Registers_Text, value);
    }
    [Category("OCPP")]
    public string _oCPP_Server_Text = string.Empty;
    public string OCPP_Server_Text
    {
        get => _oCPP_Server_Text;
        set => SetProperty(ref _oCPP_Server_Text, value);
    }
    [Category("充电桩")]
    public string _stake_Text = string.Empty;
    public string Stake_Text
    {
        get => _stake_Text;
        set => SetProperty(ref _stake_Text, value);
    }
    [Category("其他")]
    public string _otherText = string.Empty;
    public string OtherText
    {
        get => _otherText;
        set => SetProperty(ref _otherText, value);
    }
    [Category("其他")]
    public string _hTTP_Identity_Verification_Enable_Text = string.Empty;
    public string HTTP_Identity_Verification_Enable_Text
    {
        get => _hTTP_Identity_Verification_Enable_Text;
        set => SetProperty(ref _hTTP_Identity_Verification_Enable_Text, value);
    }
    [Category("HTTP 用户账号")]
    public string _httpUserNameText = string.Empty;
    public string HttpUserNameText
    {
        get => _httpUserNameText;
        set => SetProperty(ref _httpUserNameText, value);
    }
    [Category("HTTP 用户账号")]
    public string _httpUserPasswordText = string.Empty;
    public string HttpUserPasswordText
    {
        get => _httpUserPasswordText;
        set => SetProperty(ref _httpUserPasswordText, value);
    }
    [Category("充电桩OCPP")]
    public string _chargePointModelText = string.Empty;
    public string ChargePointModelText
    {
        get => _chargePointModelText;
        set => SetProperty(ref _chargePointModelText, value);
    }
    [Category("充电桩供应商")]
    public string _chargePointVendorText = string.Empty;
    public string ChargePointVendorText
    {
        get => _chargePointVendorText;
        set => SetProperty(ref _chargePointVendorText, value);
    }
    [Category("二维码1")]
    public string _qR_Code_1_Text = string.Empty;
    public string QR_Code_1_Text
    {
        get => _qR_Code_1_Text;
        set => SetProperty(ref _qR_Code_1_Text, value);
    }
    [Category("二维码2")]
    public string _qR_Code_2_Text = string.Empty;
    public string QR_Code_2_Text
    {
        get => _qR_Code_2_Text;
        set => SetProperty(ref _qR_Code_2_Text, value);
    }
    [Category("二维码3")]
    public string _qR_Code_3_Text = string.Empty;
    public string QR_Code_3_Text
    {
        get => _qR_Code_3_Text;
        set => SetProperty(ref _qR_Code_3_Text, value);
    }
    [Category("CA证书导入")]
    public string _cA_Certificate_Import_Text = string.Empty;
    public string CA_Certificate_Import_Text
    {
        get => _cA_Certificate_Import_Text;
        set => SetProperty(ref _cA_Certificate_Import_Text, value);
    }
    [Category("文件导入")]
    public string _openFileText = string.Empty;
    public string OpenFileText
    {
        get => _openFileText;
        set => SetProperty(ref _openFileText, value);
    }
    #endregion

    public ServerRegistersViewModel()
    {
        Server_Registers_Text = MainWindowViewModel.SetLanguage(MainWindowViewModel.globellanguage, "Server Regisiter");
        OCPP_Server_Text = MainWindowViewModel.SetLanguage(MainWindowViewModel.globellanguage, "OCPP Server (URL+CPID)");
        Stake_Text = MainWindowViewModel.SetLanguage(MainWindowViewModel.globellanguage, "Stake");
        OtherText = MainWindowViewModel.SetLanguage(MainWindowViewModel.globellanguage, "Other");
        HTTP_Identity_Verification_Enable_Text = MainWindowViewModel.SetLanguage(MainWindowViewModel.globellanguage, "HTTP Identity Verification Enable");
        HttpUserNameText = MainWindowViewModel.SetLanguage(MainWindowViewModel.globellanguage, "Http UserName");
        HttpUserPasswordText = MainWindowViewModel.SetLanguage(MainWindowViewModel.globellanguage, "Http UserPassword");
        ChargePointModelText = MainWindowViewModel.SetLanguage(MainWindowViewModel.globellanguage, "ChargePointModel (OCPP)");
        ChargePointVendorText = MainWindowViewModel.SetLanguage(MainWindowViewModel.globellanguage, "ChargePointVendor");
        QR_Code_1_Text = MainWindowViewModel.SetLanguage(MainWindowViewModel.globellanguage, "QR_Code 1");
        QR_Code_2_Text = MainWindowViewModel.SetLanguage(MainWindowViewModel.globellanguage, "QR_Code 2");
        QR_Code_3_Text = MainWindowViewModel.SetLanguage(MainWindowViewModel.globellanguage, "QR_Code 3");
        CA_Certificate_Import_Text = MainWindowViewModel.SetLanguage(MainWindowViewModel.globellanguage, "CA Certificate Import");
        OpenFileText = MainWindowViewModel.SetLanguage(MainWindowViewModel.globellanguage, "Open File");

        OCPPServer = MainWindowViewModel.pTAConfigModel!.OCPP服务器URL!;
        HTTPChecked = MainWindowViewModel.pTAConfigModel!.HTTP身份校验使能 ?? false;
        HttpUserName = MainWindowViewModel.pTAConfigModel!.HTTP身份校验用户名!;
        HttpUserPassword = MainWindowViewModel.pTAConfigModel!.HTTP身份校验密码!;
        ChangePointModel = MainWindowViewModel.pTAConfigModel!.ChargePointModel_OCPP!;
        ChargePointVendor = MainWindowViewModel.pTAConfigModel!.ChargePointVendor!;
        QR_Code_1 = MainWindowViewModel.pTAConfigModel!.QR_Code_1!;
        QR_Code_2 = MainWindowViewModel.pTAConfigModel!.QR_Code_2!;
        QR_Code_3 = MainWindowViewModel.pTAConfigModel!.QR_Code_3!;
        AddressText = MainWindowViewModel.pTAConfigModel!.服务器CA证书文件名!;
    }
}
